package com.airconditioning;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.junit.Test;

public class CodeGenerator {
    @Test
    public void generateCode() {
        //顶级包名
        String packageName = "com.wohome.iot";
        boolean serviceNameStartWithI = true;//user -> UserService, 设置成true: user -> IUserService
        generateByTables(serviceNameStartWithI, packageName, "users", "roles");
    }
    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        //待生成表
        //修改替换成你需要的表名，多个表名传数组
        String[] str = new String[]{"enum"};
        GlobalConfig config = new GlobalConfig();
        //数据库连接
        String dbUrl = "jdbc:mysql://localhost:3306/iot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false";
        StrategyConfig strategyConfig = new StrategyConfig();
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        String projectPath = System.getProperty("user.dir");
        config.setOutputDir(projectPath + "/src/main/java");
        config.setOpen(false);//不打开目录
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("123456")
                .setDriverName("com.mysql.cj.jdbc.Driver");

        strategyConfig
                .setCapitalMode(true)
                .setEntityTableFieldAnnotationEnable(true)
                .setEntityLombokModel(true)
                .setEntityBuilderModel(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(str);

        config.setActiveRecord(true) // 类似于建造者模式直接 实体类点 dao 的操作方法
                .setAuthor("zhouyang");
        config.setBaseResultMap(true);

        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController("templates/controller.java");
        templateConfig.setService("templates/IService.java");
        templateConfig.setXml("/templates/mapper.xml.vm");
        templateConfig.setMapper("/templates/mapper.java");

        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                //.setCfg(cfg)
                .setTemplate(templateConfig)
                .setTemplateEngine(new VelocityTemplateEngine())
                .setPackageInfo(
                        new PackageConfig() //设置此项目包名
                                .setParent("com")
                                .setModuleName("wohome.iot")
                                .setController("controller")
                                .setEntity("entity")
                                .setMapper("mapper")
                ).execute();
    }
}
